/*
**************************************************************************************
*Myriad Engine                                                                       *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
package leola.types;

import java.lang.reflect.Method;

import leola.backend.interpreter.Scope;
import leola.backend.interpreter.SymbolStack;
import leola.backend.interpreter.SymbolTable;
import leola.utils.ClassUtil;


/**
 * A {@link LeoScopedObject} contains a local {@link SymbolStack} for support of
 * closures and class instances.  The local {@link SymbolStack} allows for persistent storage
 * of variables that are local to the closure or class object.
 * 
 * @author Tony
 *
 */
public abstract class LeoScopedObject extends LeoObject {

	/**
	 * The local symbol stack for this object
	 */
	private Scope scope;
	
	/**
	 * @param type
	 * @param parent
	 */
	public LeoScopedObject(LeoType type, Scope parent) {
		super(type);
		
		/* If this scope belongs to a class, don't clone it
		 * other wise we must clone to support proper closures 
		 */
		this.scope = parent;//.isClassScope() ? parent : parent.clone();
		this.scope.setOwner(this);
	}
	

	/**
	 * @return the scope
	 */
	public Scope getScope() {
		return scope;
	}
	
	/* (non-Javadoc)
	 * @see leola.types.LeoObject#isScopedObject()
	 */
	@Override
	public boolean isScopedObject() {
		return true;
	}
	
	/**
	 * Adds a method to the class scope.
	 * 
	 * @param method
	 */
	protected void addMethod(String methodName, Class<?> ... params) {
		Method method = ClassUtil.getMethodByName(getClass(), methodName, params);
		if ( method == null ) {
			throw new IllegalArgumentException("Invalid method name: " + methodName);
		}
		
		SymbolTable table = getScope().getSymbolTable();
		table.put(method.getName(), new LeoNativeFunction(this, method));
	}
}
